package com.controller;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.entity.Register;
import com.service.RegisterService;

@Controller
public class controller4 {
	@Resource
	public RegisterService registerService;
	
	public List<Register> registerlist;
	
	/*功能一: 分页查询
	 * bespeakpage  >>>>>  获取页面信息并分页
	 * 
	 * bespeakpage2  >>>>  上一页
	 * 
	 * bespeakpage3  >>>>  下一页
	 * 
	 * */	
//  获取页面信息并分页
	@RequestMapping(value="registerpage",method=RequestMethod.GET)
	public String registerpage(Model model){
		registerlist = registerService.selefen(0);
		
		int ys=0;
		int ys1=1;
		model.addAttribute("registerlist", registerlist);
		model.addAttribute("ys", ys);
		model.addAttribute("ys1", ys1);
		return "registerpage";
	}
//	上一页
	@RequestMapping(value="registerpage2",method=RequestMethod.GET)
	public String registerpage2(int ys,int ys1,Model model){
		
		registerlist = registerService.selefen(ys*5);
		
		if(ys==0){
			model.addAttribute("registerlist", registerlist);
			model.addAttribute("ys", ys);
			model.addAttribute("ys1", ys1);
			return "registerpage";
		}
		ys1=ys1-1;
		ys=ys-1;
		model.addAttribute("registerlist", registerlist);
		model.addAttribute("ys", ys);
		model.addAttribute("ys1", ys1);
		return "registerpage";
	}
	
//	下一页
	@RequestMapping(value="registerpage3",method=RequestMethod.GET)
	public String registerpage3(int ys,int ys1,Model model){
		registerlist = registerService.selefen(ys1*5);
		
		if(registerlist!=null&&!registerlist.isEmpty()){
			ys1=ys1+1;
			ys=ys+1;

			model.addAttribute("registerlist", registerlist);
			model.addAttribute("ys", ys);
			model.addAttribute("ys1", ys1);
			return "registerpage";
			
			
		}else{
			model.addAttribute("registerlist", registerlist);
			model.addAttribute("ys", ys);
			model.addAttribute("ys1", ys1);
			model.addAttribute("ts", "没有了请返回上一页");
			return "registerpage";
		}
		
	}
	
//	按条件查询
	@RequestMapping(value="registerselect",method=RequestMethod.POST)
	public String registerselect(String rname,Model model){
	
		registerlist = registerService.select(rname);
		
		model.addAttribute("registerlist", registerlist);
		
		return "registerpage";
	}
	
//	跳转到添加页面
	@RequestMapping("insert_register")
	public String insert_register(){
		
		return "insert_register";
	}
	
//	添加图片测试
	@RequestMapping(value="insert_registerimpl",method=RequestMethod.POST)
	public String insert_registerimpl(HttpServletRequest request,Model model){
	
		boolean isMutiPart = ServletFileUpload.isMultipartContent(request);
		
		String uploadpath = request.getSession().getServletContext().getRealPath("jpg/");
		
		System.out.println(uploadpath);
		
		File saveDir = new File(uploadpath);
		
		if(!saveDir.exists()){
			saveDir.mkdir();
			
		}
		
		if(isMutiPart){
			
			FileItemFactory factory = new DiskFileItemFactory();
			ServletFileUpload upload = new ServletFileUpload(factory);
			
			try {
				
				List<FileItem> items = upload.parseRequest(request);
				Iterator<FileItem> iter = items.iterator();
				while(iter.hasNext()){
					FileItem item = iter.next();
					
					if(item.isFormField()){
//						String fieldName = item.getFieldName();
//						
//						if(fieldName.equals("pname")){
//							
//							photo.setPname(item.getString("UTF-8"));
//							
//						}else if(fieldName.equals("aid")){
//							
//							photo.setAid(Integer.parseInt(item.getString()));
//						}
						System.out.println("111");
					}else{
						String url = item.getName();
						if(url !=null && !url.equals("")){
							
							File full = new File(url);
							File sfull = new File(uploadpath,full.getName());
							item.write(sfull);
						}
						
					}
					
				}
				
			} catch (FileUploadException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		registerlist = registerService.selefen(0);
		
		int ys=0;
		int ys1=1;
		model.addAttribute("registerlist", registerlist);
		model.addAttribute("ys", ys);
		model.addAttribute("ys1", ys1);
		return "registerpage";
	}
}
